#include "../include/GeneralConfiguration.h"
#include <sstream>
#include "Poco/DateTime.h"
#include "Poco/DateTimeParser.h"
#include <string>
using namespace Poco;
using namespace Poco::Util;
using namespace std;

GeneralConfiguration::GeneralConfiguration()
{
}

GeneralConfiguration::~GeneralConfiguration()
{
}

// Constructor that receive data stream as std::istream
GeneralConfiguration::GeneralConfiguration(std::istream& istr) : IniFileConfiguration(istr)
{
        Construct();
}

// Constructor that receive path as std::string
GeneralConfiguration::GeneralConfiguration(const std::string& path) : IniFileConfiguration(path)
{
	Construct();
}

void GeneralConfiguration::Construct()
{
		try{
			if (hasProperty("GENERAL.USER_ACCOUNT")){
				ostringstream accountKeyNameBuilder;
				accountKeyNameBuilder << "GENERAL" << ".USER_ACCOUNT";
				double account = getDouble(accountKeyNameBuilder.str());
				bAccount = account;
			}
			else{
				bAccount = 100;
			}

			if (hasProperty("GENERAL.ENERGY_VALUE")){
				ostringstream valueKeyNameBuilder;
				valueKeyNameBuilder << "GENERAL" << ".ENERGY_VALUE";
				double eValue = getDouble(valueKeyNameBuilder.str());
				energyValue = eValue;
			}
			else
				energyValue = 100;

			if (hasProperty("GENERAL.SAYA_ON")){
				ostringstream sayaKeyNameBuilder;
				sayaKeyNameBuilder << "GENERAL" << ".SAYA_ON";
				string saya = getString(sayaKeyNameBuilder.str());
				bool agentOn;
				if (saya == "ON")
					agentOn = true;
				else
					if (saya == "OFF")
						agentOn = false;

				AgentOn = agentOn;
			}
			else
				AgentOn = true;

			if (hasProperty("GENERAL.SAYA_BUYING")){
				ostringstream sayabKeyNameBuilder;
				sayabKeyNameBuilder << "GENERAL" << ".SAYA_BUYING";
				int sayaBuy = getInt(sayabKeyNameBuilder.str());
				sayaBuying = sayaBuy;
			}
			else
				sayaBuying = 20;

			if (hasProperty("GENERAL.LOGGER_CONSOLE_PRIORITY")){
				ostringstream LCPKeyNameBuilder;
				LCPKeyNameBuilder << "GENERAL" << ".LOGGER_CONSOLE_PRIORITY";
				int LCP = getInt(LCPKeyNameBuilder.str());
				loggerCP = LCP;
			}
			else
				loggerCP = 6;

			if (hasProperty("GENERAL.LOGGER_FILE_PRIORITY")){
				ostringstream LFPKeyNameBuilder;
				LFPKeyNameBuilder << "GENERAL" << ".LOGGER_FILE_PRIORITY";
				int LFP = getInt(LFPKeyNameBuilder.str());
				loggerFP = LFP;
			}
			else
				loggerFP =5;
		}
		catch (Exception ex)
        {
    		std::ostringstream strStream ;
    		strStream << "Error reading configuration file.";
    		EnergyMarketLogger::Instance().Log(strStream , Poco::Message::PRIO_CRITICAL);
        }
}

double GeneralConfiguration::getAccount(){
	return bAccount;
}
double GeneralConfiguration::getEnergyValue(){
	return energyValue;
}
bool GeneralConfiguration::getAgentOn(){
	return AgentOn;
}
int GeneralConfiguration::getSayaBuying(){
	return sayaBuying;
}
int GeneralConfiguration::getLoggerCP(){
	return loggerCP;
}
int GeneralConfiguration::getLoggerFP(){
	return loggerFP;
}
